package b1.scenes.suspect
{
	import b1.conifg.*;
	import b1.controls.basic.AnimalMc;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.SceneContainer;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.ui.Mouse;
	import flash.utils.Timer;

	public class SuspectAnimal extends Sprite
	{
		private var animalSprite:AnimalMc;
		//statusOrder模3后为动物状态在数组statusArray中的位置
		private var statusOrder:int;
		//动物的状态;对应animalMc里面的状态号
		private const statusArray:Array = [0, 3, 4];
		private var animalConfig:AnimalConfig;
		private var animal:AnimalInfo;
		//存放热点
		private var showPointsSprite:Sprite;
		private var changeStatusTimer:Timer;
		private var clickFlag:Boolean;
		
		//statusOrder中，0为正面，1为左站立; 2为右站立
		public function SuspectAnimal(animal:AnimalInfo, statusOrder:int)
		{
			super();
			this.animal = animal;
			animalSprite = new AnimalMc(animal.id);
			var animalSpriteDiag:Number = Math.sqrt(animalSprite.width * animalSprite.width + animalSprite.height * animalSprite.height);
			var ratio:Number = 350 / animalSpriteDiag;
//			animalSprite.width = animalSprite.width * ratio;
//			animalSprite.height = animalSprite.height * ratio;
			animalSprite.scaleX = ratio;
			animalSprite.scaleY = ratio;
//			animalSprite.height = 130;
//			animalSprite.scaleX = animalSprite.scaleY;
			animalSprite.status = statusOrder;
//			animalSprite.x = - animalSprite.width / 2;
//			animalSprite.y = - animalSprite.height;
			addChild(animalSprite);
			clickFlag = false;
			this.statusOrder = statusOrder;
			animalConfig = Config.animalConfigs[animal.animalIndex];
			//显示线索点
			showPoints(statusArray[statusOrder % 3]);
			
			changeStatusTimer = new Timer(3000);
			changeStatusTimer.start();
			changeStatusTimer.addEventListener(TimerEvent.TIMER, onChangeStatus);
			
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			
			
			//单击转换侧面
			this.addEventListener(MouseEvent.CLICK, onMouseClick);	
		}
		
		//timer停止
		public function stopTimer():void
		{
			if (null != changeStatusTimer)
			{
				this.changeStatusTimer.removeEventListener(TimerEvent.TIMER, onChangeStatus);
				this.changeStatusTimer.stop();
			}
		}
		
		
		//转换侧面
		private function onChangeStatus(event:Event):void
		{
			if(clickFlag == false)
			{
				statusOrder++;
				var animalStatus:int  = statusArray[(statusOrder % 3)];
				setAnimalStatus(animalStatus);
//				animalSprite.status = animalStatus;
//				animalSprite.x = - animalSprite.width / 2;
//				animalSprite.y = - animalSprite.height;
				showPoints(animalStatus);
			}
		}
		//改变动物的状态
		public function setAnimalStatus(animalStatus:int):void
		{
			animalSprite.status = animalStatus;
		}
		//显示线索点
		private function showPoints(animalStatus:int):void
		{
			if (null != showPointsSprite && null != showPointsSprite.parent)
			{
				this.removeChild(showPointsSprite);
			}
			showPointsSprite = new Sprite();
			for (var clueIndex:String in animalConfig.animalClues)
			{
				var animalClue:AnimalClueConfig = animalConfig.animalClues[clueIndex];
				for (var i:int = 0; i < animalClue.showPoints.length; i++)
				{
					var showPoint:HotPoint = animalClue.showPoints[i];
					if (showPoint.imageIndex == animalStatus)
					{
						var cluePoint:CluePoint = new CluePoint(parseInt(clueIndex), animal.animalClues[clueIndex]);
						cluePoint.x = showPoint.x * animalSprite.width / 100 + animalSprite.x - cluePoint.width / 2;
						cluePoint.y = showPoint.y * animalSprite.height / 100 + animalSprite.y - cluePoint.height / 2;
						showPointsSprite.addChild(cluePoint);
					}
				}
			}
			this.addChild(showPointsSprite);
		}
		
			
		public function onMouseClick(event:MouseEvent):void
		{
			statusOrder++;
			var animalStatus:int  = statusArray[(statusOrder % 3)];
			animalSprite.status = animalStatus;
			showPoints(animalStatus);
		}
		
		public function onMouseOver(event:MouseEvent):void
		{
			clickFlag = true;
			Mouse.cursor = "button";
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
		}

	}
}